﻿Imports System.Text.RegularExpressions

Public Class ProjectBasicInfoForm

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Me.Text = "增加工程"
        Me.proIDtext.Focus()
    End Sub

    Public Sub New(ByVal pi As ProjectItem)
        MyClass.New()

        _pi = pi
        modefy = True

        Me.Text = "修改工程"
    End Sub

    Private modefy As Boolean
    Private _pi As ProjectItem = Nothing
    Private Const code As String = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"

    Private Sub ProjectBasicInfoForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If _pi IsNot Nothing Then
            LoadInfo()
            'LoadConnectConfig(_pi) 'Add by ZhangTeng
        End If
    End Sub

    Private Sub LoadInfo()
        Me.proIDtext.Text = _pi.ProjectID
        Me.txtProjectName.Text = _pi.ProjectName
        Me.nudOrder.Value = _pi.listNo
        Me.txtRemark.Text = _pi.Remark
        If _pi.AuthCode.Trim.Length > 0 Then Me.txtMacID.Text = code
        Me.txtMacID.UseSystemPasswordChar = (_pi.AuthCode.Trim.Length > 0)
    End Sub


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If CheckValidate() = False Then
            Exit Sub
        End If

        Dim pi As ProjectItem = Nothing

        If _pi Is Nothing Then
            pi = New ProjectItem()
        Else
            pi = _pi
        End If
        pi.ProjectID = Me.proIDtext.Text.Trim

        pi.ProjectName = Me.txtProjectName.Text.Trim
        pi.Remark = Me.txtRemark.Text.Trim
        pi.VersionNo = g_version
        pi.listNo = Me.nudOrder.Value
        If Me.txtMacID.Text.Equals(code) = False Then pi.AuthCode = Me.txtMacID.Text.Replace("-", String.Empty)
        'pi.ComNumber = Me.nudCOM.Value

        If _pi Is Nothing Then
            Try
                ProjectService.CreateBasicInfo(pi)
            Catch ex As Exception
                msg.ShowError(ex.Message)
                Exit Sub
            End Try

            msg.Show("增加工程成功。")

        Else
            Try
                ProjectService.UpdateBasicInfo(pi)
            Catch ex As Exception
                msg.ShowError(ex.Message)
                Exit Sub
            End Try

            msg.Show("修改工程成功。")

        End If

        'SaveConnectConfig(pi) 'Add by ZhangTeng

        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()

    End Sub

    Private Function CheckValidate() As Boolean
        Dim PrjID As Boolean
        If Me.txtProjectName.Text.Trim = "" Then
            msg.Show("工程名称不能为空。")
            Me.txtProjectName.Focus()
            Return False
        End If

        If txtMacID.Text.Trim.Length <= 0 Then
            msg.ShowError("鉴权编码不能为空。")
            Me.txtMacID.Focus()
            Return False
        End If

        ' Dim str As String = txtMacID.Text.Trim.Chars(-1)
        'If txtMacID.Text.Trim.Chars(0) = " " Then
        '    msg.ShowError("鉴权编码中不能包含空格。")
        '    Me.txtMacID.Focus()
        '    Return False

        'End If

        If Me.proIDtext.Text.Trim.Length > 4 Then
            msg.ShowError("输入工程ID不正确。")
            Me.proIDtext.Focus()
            Return False
        End If
        'If optTcp.Checked Then
        '    For Each p As String In _ConnectParameters
        '        If p Is Nothing Then
        '            msg.ShowError("请配置连接参数")
        '            Me.btnConnectConfig.Focus()
        '            Return False
        '        End If
        '    Next
        'End If
        If modefy Then
        Else
            PrjID = ProjectService.GetPrjID(Me.proIDtext.Text)

            '判断工程中是否含有相同工程ID
            If PrjID Then
                msg.ShowError("项目中已经包含相同工程ID。")
                Me.proIDtext.Focus()
                Return False
            End If

        End If

       



        Return True
    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub txtIPPort_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = False
        If Regex.IsMatch(e.KeyChar, "^/.$") OrElse (Not Char.IsDigit(e.KeyChar) AndAlso Asc(e.KeyChar) <> Keys.Back AndAlso Asc(e.KeyChar) <> Keys.Delete) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtIPAddress_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = False
        If Not Char.IsNumber(e.KeyChar) AndAlso Asc(e.KeyChar) <> Keys.Back AndAlso Asc(e.KeyChar) <> Keys.Delete Then
            e.Handled = True
        End If
    End Sub

    'Private Sub btnConnectConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Dim frm As New ConnectConfigForm(_ConnectParameters)
    '    If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
    '        _isChangedCP = True
    '    End If
    'End Sub

    '-----------------2008-11-21--------------------
    '----------------连接参数设置-------------------
    'Private _ConnectParameters(2) As String
    'Private _isChangedCP As Boolean = False
    'Private Sub LoadConnectConfig(ByVal pi As ProjectItem)
    '    _ConnectParameters(0) = KeyValuePairClass.GetValue(pi.ProjectID, "ClientSocket_Heartbeat_IdleTime")
    '    _ConnectParameters(1) = KeyValuePairClass.GetValue(pi.ProjectID, "ClientSocket_Heartbeat_WaitTime")
    '    _ConnectParameters(2) = KeyValuePairClass.GetValue(pi.ProjectID, "ClientSocket_Heartbeat_Rep")
    'End Sub
    'Private Sub SaveConnectConfig(ByVal pi As ProjectItem)
    '    If _isChangedCP Then
    '        KeyValuePairClass.SetValue(pi.ProjectID, "ClientSocket_Heartbeat_IdleTime", _ConnectParameters(0))
    '        KeyValuePairClass.SetValue(pi.ProjectID, "ClientSocket_Heartbeat_WaitTime", _ConnectParameters(1))
    '        KeyValuePairClass.SetValue(pi.ProjectID, "ClientSocket_Heartbeat_Rep", _ConnectParameters(2))
    '    End If
    'End Sub
    '---------------Add by ZhangTeng-----------------

    Private Function GetShowAuthCode(ByVal code As String) As String
        Dim ret As String = String.Empty
        Dim length As Integer = code.Length / 4 - 1

        For i As Integer = 0 To length
            ret &= code.Substring(i * 4, 4)
            If i < length Then
                ret &= "-"
            End If
        Next

        Return ret
    End Function

    Private Sub txtMacID_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtMacID.Validating

        txtMacID.UseSystemPasswordChar = (txtMacID.TextLength > 0)

    End Sub

    Private Sub txtMacID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMacID.TextChanged

        If txtMacID.UseSystemPasswordChar = True Then
            txtMacID.UseSystemPasswordChar = (txtMacID.TextLength > 0)
        End If

    End Sub

    Private Sub txtMacID_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles txtMacID.PreviewKeyDown

        If txtMacID.SelectionLength = txtMacID.TextLength Then
            txtMacID.Text = String.Empty
        End If

    End Sub
End Class